<?php

/**
 * For constructing unordered lists that will be used as a drop-down menu, with the right CSS.
 */
class MenuHelper extends AppHelper {
  
  var $helpers = array('Html');
  
  
  function __construct($options = null) {
  
    parent::__construct($options);
    
  } ///////////// end constructor /////////////////
  
  
  /**
   *
   * @param string $linkToController
   * @param string $linkToMethod
   * @param bool   $isFirstTopMenuItem 
   * @param bool    $isLastTopMenuItem
   * @param string $linkDisplayStr  A string that you want displayed as the link text.
   * @param string $tooltip         Text to be displayed on hover on the item.
   * @param string $iconPath        Relative to 'webroot/img'
   * @param string $nestedListItem  A menu list to be 'under' this item. The list needs to be another
   *                                <ul> created the same way as this list is being created.
   * @return string        The <li> with child <a> and display text, etc. 
   */
  function makeListItem($linkToController, $linkToMethod, 
                        $isFirstTopMenuItem = false, $isLastTopMenuItem = false,
                        $linkDisplayStr = null, $tooltip = null, 
                        $iconPath = null, $nestedList = null ) {
    
    $options = array();
    if($linkDisplayStr) {
      $options['alt'] = $linkDisplayStr;
    }
    else if($tooltip) {
      $options['alt'] = $tooltip;
    }
    
    if($iconPath) {
      $iconHtml = $this->Html->image($iconPath, $options);   
    }
    else {
      $iconHtml = '';
    }
    
    $linkToArray = array('controller'=>$linkToController, 'action'=>$linkToMethod );
    if($linkToController==null || $linkToController== '') {
      $linkToArray = array('#' => ''); // just link to same page user is on (don't leave it)
    }
    
    
    $linkOptions = array('title'=>$tooltip, 'escape' => false);
    
    if(!$linkDisplayStr) {
      $linkDisplayStr = '';
    }
    
    $entireLinkContents = "{$iconHtml}$linkDisplayStr";
    // Wrap a 'parent' anchor (has submenu elements) contents in a <span> element.
    if($nestedList) {
      $entireLinkContents = "<span>$entireLinkContents</span>";
    }
    
    $linkHtml = $this->Html->link($entireLinkContents, $linkToArray, $linkOptions);
    
    $listItemContents = '';
    // If a nested list html str was passed in, need to make it a sibling to the link html
    if($nestedList) {
      $listItemContents = "$linkHtml\n$nestedList";
    }
    else {
      $listItemContents = $linkHtml;
    }
    
    
    if($isFirstTopMenuItem) {
      return $listItemHtml = "<li class='topfirst'>$listItemContents</li>";
    }
    else if($isLastTopMenuItem) {
      return $listItemHtml = "<li class='toplast'>$listItemContents</li>";
    }
    else {
      return $listItemHtml = "<li>$listItemContents</li>";
    }
    
  } ////////// end makeListItem() //////////////////
  
  

} //////////////////////// end class ////////////////////////////////

?>
